package com.hotel.controller;

import com.hotel.pojo.Admin;
import com.hotel.pojo.Staff;
import com.hotel.service.AdminService;
import com.hotel.vo.AdminPassVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    // 管理员登录
    @PostMapping("login/{name}")
    public String adminLogin(@PathVariable("name") String name,
                             @RequestBody AdminPassVo adminPassVo) {
        Admin admin = new Admin();
        admin.setName(name);
        admin.setUsername(adminPassVo.getUsername());
        admin.setPassword(adminPassVo.getPassword());
        if(adminService.checkAdmin(admin)) {
            if (adminService.isLogin(name)) {
                return "请勿重复登陆！";
            } else {
                adminService.loginAdmin(name);
                return "管理员登陆成功！";
            }
        } else {
            return "账号或者密码错误！";
        }
    }

    // 管理员注销
    @PostMapping("exit/{name}")
    public String adminExit(@PathVariable("name") String name) {
        if(adminService.findAdminByName(name)){
            adminService.exitAdmin(name);
            return "注销成功！";
        } else {
            return "管理员不存在！";
        }
    }

    // 注册酒店工作人员账号
    @PostMapping("{adminname}/register")
    public String registerStaff(@PathVariable("adminname") String adminName,
                                @RequestBody AdminPassVo adminPassVo) {
        if (!adminService.isLogin(adminName)) {
            return "请先登录！";
        }
        if(!adminService.findAdminByName(adminName)) {
            return "管理员不存在，注册酒店工作人员账号失败！";
        } else {
            Staff staff = new Staff();
            staff.setName(adminPassVo.getStaffName());
            staff.setUsername(adminPassVo.getUsername());
            staff.setPassword(adminPassVo.getPassword());
            staff.setStatus(0);
            boolean b = adminService.registerStaff(staff);
            if (b) {
                return "注册酒店工作人员账号成功！";
            } else {
                return "用户名或姓名已存在，注册失败！";
            }
        }
    }

    // 更改管理员密码
    @PostMapping("{adminname}/update")
    public String updateAdmin(@PathVariable("adminname") String adminName,
                              @RequestBody AdminPassVo adminPassVo) {
        if (!adminService.isLogin(adminName)) {
            return "请先登录！";
        }
        if(!adminService.findAdminByName(adminName)) {
            return "管理员不存在，更改管理员密码失败！";
        } else {
            Admin admin = new Admin();
            admin.setName(adminName);
            admin.setUsername(adminPassVo.getUsername());
            admin.setPassword(adminPassVo.getPassword());
            adminService.updateAdmin(admin);
            return "更改管理员密码成功！";
        }
    }
}
